package com.servlet;


import com.model.Users;
import com.service.UsersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
    UsersService usersService = new UsersService();
    HttpServletRequest request;
    HttpServletResponse response;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("user_name");
        String password = request.getParameter("password");
        //String cb = request.getParameter("cb");
        System.out.println("账号：" + username);
        System.out.println("密码：" + password);

        // 将判断用户正确与否转交给Service去做
        username = usersService.login(username, password);
        if (username != null) {

            //创建session 给session赋值
            request.getSession().setAttribute("username", username);

            /*if ("cb".equals(cb)) {
                //创建cookie
                Cookie cookie_username = new Cookie("username", username);
                Cookie cookie_password = new Cookie("password", password);
                //设置cookie存活时间
                cookie_username.setMaxAge(60 * 2);
                cookie_password.setMaxAge(60 * 2);
                //响应到客户端
                response.addCookie(cookie_password);
                response.addCookie(cookie_username);
            }*/
            //将参数传递给页面
            request.setAttribute("err", username);
            //页面跳转
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else {
            //将参数传递给页面
            request.setAttribute("err", "用户名或密码错误");
            //页面跳转
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受页面的判断me,判断当前方法执行什么内容
        this.request=request;
        this.response=response;
        String me = request.getParameter("method");
        if(me.equals("show")){
            this.show();
        }
        //添加数据
        if (me.equals("add")){
            this.add();
        }
        //删除数据
        if (me.equals("delete")){
            this.delete();
        }
        //修改数据
        if (me.equals("toupdate")){
            this.toupdate();
        }
        if (me.equals("update")){
            this.update();
        }
    }

    private void update() throws IOException {
        String user_id=request.getParameter("user_id");
        String user_name=request.getParameter("user_name");
        String user_password=request.getParameter("user_password");
        String user_email = request.getParameter("email");
        String user_ctime = request.getParameter("user_ctime");

        usersService.update(user_id,user_name,user_password,user_email,user_ctime);
        //response  对象向页面响应
        //response.getWriter().print("<script>parent.showStudent()</script>");
    }

    private void toupdate() throws ServletException, IOException {
        String id = request.getParameter("id");
        Users users= usersService.toupdate(id);
        request.setAttribute("users",users);
        request.getRequestDispatcher("jsp/users/update.jsp").forward(request,response);
    }

    private void delete() throws ServletException, IOException {
        String id=request.getParameter("id");
        usersService.delete(id);
        this.show();
    }

    private void add() throws IOException, ServletException {
        String user_name=request.getParameter("user_name");
        String user_password=request.getParameter("password");
        String user_email = request.getParameter("email");
        String user_ctime = request.getParameter("user_ctime");
        usersService.add(user_name,user_password,user_email,user_ctime);
    }

    private void show() throws ServletException, IOException {
        //从service中获取集合数据为db中的
        List<Users> userss = usersService.show();
        //将数据传递给页面
        request.setAttribute("userss",userss);
        //跳转到指定的页面
        request.getRequestDispatcher("jsp/users/show.jsp").forward(request,response);
    }
}
